package com.quectel.cms.controller.car;

import com.quectel.cms.controller.BaseController;
import com.quectel.constant.core.ResourceGroupCodeConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.device.dto.DeviceDto;
import com.quectel.core.module.device.service.DeviceCategoryService;
import com.quectel.core.module.device.service.DeviceService;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-11-18 16:05:44
 */
@RestController
@RequestMapping("carVillageDevice")
@Tag(name = "CarVillageController", description = "停车道闸设备管理相关api")
public class CarVillageController extends BaseController {

    @DubboReference
    private DeviceService deviceService;
    @DubboReference
    private DeviceCategoryService deviceCategoryService;

    @DubboReference
    private VillageService villageService;

    /**
     * 列表
     */
    @GetMapping("list")
    @RequirePermissions(values = "carVillageDevice:list")
    @Operation(summary = "停车道闸设备的分页查询列表")
    public Response<Page<DeviceDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest)).like("name");

        super.fillAuthorityData(request.getParams());
        super.fillResourceGroupCode(request.getParams(), ResourceGroupCodeConstants.CAR_GATE_GROUP);

        int total = deviceService.queryTotal(request.getParams());

        List<DeviceDto> list = null;
        if (total > 0) {
            list = deviceService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<DeviceDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<DeviceDto>>ok().wrap(page);
    }

}
